package com.zhihaoliang.room.data.relationships

import androidx.room.*

/**
 * @author 支豪亮
 * @date 2023/1/31 16:40
 * @des 定义一对多关系
 */

@Entity
data class UserPlay(
    @PrimaryKey val userId: Long,
    val name: String,
    val age: Int
)

@Entity
data class Playlist(
    @PrimaryKey val playlistId: Long,
    val userCreatorId: Long,
    val playlistName: String
)

data class UserWithPlaylists(
    @Embedded val user: UserPlay,
    @Relation(
        parentColumn = "userId",
        entityColumn = "userCreatorId"
    )
    val playlists: List<Playlist>
)

@Dao
interface OneToManyDao{

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUserPlay(list:List<UserPlay>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertPlayList(list: List<Playlist>)

    @Transaction
    @Query("SELECT * FROM UserPlay")
    suspend fun queryUsersWithPlaylists(): List<UserWithPlaylists>
}